#include <iostream>
#include <vector>
#include <unordered_map>

using namespace std;

// 2461. 长度为 K 子数组中的最大和
// https://leetcode.cn/problems/maximum-sum-of-distinct-subarrays-with-length-k/description/

class Solution {
public:
    long long maximumSubarraySum(vector<int>& nums, int k) {
        long long sum = 0;
        int left = 0;
        long long ans = 0;
        unordered_map<int, int> recordNumber;
        for (int i = 0; i < nums.size(); i++){
            sum += nums[i];
            recordNumber[nums[i]]++;
            if(i - left + 1 < k){
                continue;
            }
            if(recordNumber.size() == k){
                ans = max(ans, sum);
            }
            if(recordNumber[nums[left]] == 1){
                recordNumber.erase(nums[left]);
            }else{
                recordNumber[nums[left]]--;
            }
            sum -= nums[left++];
        }
        return ans;
    }
};

int main()
{
    return 0;
}